package com.visikard.ui.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.visikard.item.ItemAudienceKard;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;
import com.visikard.widget.NavigationStateImageView;

public class ChooseAudienceAdapter  extends BaseAdapter implements Filterable,
		SectionIndexer {

	Context context;
	ArrayList<ItemAudienceKard> data;
	ArrayList<ItemAudienceKard> dataAll;
	LayoutInflater inflater;
	DisplayMetrics metrics;
	Handler mHandler;
	
	private String listfkUser = "";
	private String listfkKard = "";

	public ChooseAudienceAdapter(Context ct, ArrayList<ItemAudienceKard> dt) {
		this.context = ct;
		this.data = dt;
		this.dataAll = dt;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.choose_audience_item, null);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();

			viewHolder.imgKard = (ImageView) convertView.findViewById(R.id.image_kard_audience);
			viewHolder.txtKardName = (TextView) convertView.findViewById(R.id.text_name_kard);
			viewHolder.btnCheck = (NavigationStateImageView) convertView.findViewById(R.id.btn_check);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final ItemAudienceKard item = (ItemAudienceKard) getItem(position);
		if (item != null) {

			if (item.getKardName() != null && item.getKardName().length() > 0)
				viewHolder.txtKardName.setText(item.getKardName());
			else
				viewHolder.txtKardName.setText("");

			int mWidth = metrics.widthPixels / 4;
			int mHeight = 3 * mWidth / 2;

			RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) viewHolder.imgKard
					.getLayoutParams();

			lyKard.width = mWidth;
			lyKard.height = mHeight;

//			ImageLoader.getInstance(context).loadImageCorner(viewHolder.imgKard, item.getImageSmall(),
//					Device.getDeviceWidth() / 3, BitmapUtils.ROUND_ALL, 4, 3, R.drawable.kard_shadow_default_vertical, 0, 0);
			UILImageUtils.loadImage(viewHolder.imgKard, //image view
					item.getImageSmall(), //url
					1, //size of view
					BitmapUtils.ROUND_ALL, //round all 
					4, // round size
					3, //shadow
					R.drawable.kard_shadow_default_vertical, //shadow resource 
					0, 0); 
			
			if (item.isCheck()) {
				listfkUser = "," + item.getFkUser();
				listfkKard = "," + item.getFkKard();
				viewHolder.btnCheck.setChecked(true);
			}else
				viewHolder.btnCheck.setChecked(false);

			viewHolder.btnCheck.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					listfkUser = "";
					listfkKard = "";
					if(data.get(position).isCheck())
						data.get(position).setCheck(false);
					else
						data.get(position).setCheck(true);
					
					notifyDataSetChanged();
				}
			});
		}
		return convertView;
	}

	private class ViewHolder {
		TextView txtKardName;
		ImageView imgKard;
		NavigationStateImageView btnCheck;
	}

	Filter filter = new Filter() {

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			data = (ArrayList<ItemAudienceKard>) results.values;
			notifyDataSetChanged();
		}

		@SuppressLint("DefaultLocale")
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults result = new FilterResults();

			ArrayList<ItemAudienceKard> filterList = new ArrayList<ItemAudienceKard>();
			if (constraint.length() > 0) {
				for (ItemAudienceKard item : dataAll) {
					if (item.getKardName().toLowerCase()
							.contains(constraint.toString().toLowerCase())) {
						filterList.add(item);
					}
				}
				result.values = filterList;
				return result;
			}

			result.values = dataAll;
			return result;
		}
	};
	
	public String getListUser(){
		return listfkUser;
	}
	
	public String getListFkKard(){
		return listfkKard;
	}

	@Override
	public Filter getFilter() {
		return filter;
	}

	@Override
	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		if (section == 35) {
			return 0;
		}
		for (int i = 0; i < data.size(); i++) {
			String l = data.get(i).getKardName();
			char firstChar = l.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}
}
